package com.southminority.ethnic.controller.admin.excel;

import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.pojo.Inheritor;
import com.southminority.ethnic.mapper.heritage.InheritorMapper;
import com.southminority.ethnic.until.ExcelUntil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@Tag(name = "excel-传承人", description = "传承人导入导出")
@RestController
@RequestMapping("/admin/inheritor-excel")
@CrossOrigin
public class InheritorExcelController {
    @Autowired
    private InheritorMapper inheritorMapper;

    @GetMapping("/output")
    @Operation(summary = "导出所有传承人", description = "导出所有传承人")
    public ResponseEntity<byte[]> outputExcel() {
        try {
            List<Inheritor> dataList = inheritorMapper.selectList(null);
            byte[] excelBytes = ExcelUntil.exportExcel(dataList);
            String fileName = "inheritor_output";
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            String encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            headers.setContentDispositionFormData("attachment", encodedFileName);
            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/mb-input")
    @Operation(summary = "获取传承人导入模版", description = "获取传承人导入模版")
    public ResponseEntity<byte[]> mbInputExcel() {
        try {
            String fileName = "inheritor_input";
            byte[] excelBytes = ExcelUntil.mbExcel(Inheritor.class);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            String encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx";
            headers.setContentDispositionFormData("attachment", encodedFileName);
            return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/input")
    @Operation(summary = "导入传承人", description = "导入传承人")
    public R<String> importAndSave(@RequestParam("file") MultipartFile file) {
        try {
            List<Inheritor> list = ExcelUntil.importExcel(file, Inheritor.class);
            for (Inheritor h : list) {
                if (h.getId() == null) inheritorMapper.insert(h); else inheritorMapper.updateById(h);
            }
            return R.ok("导入成功，共" + list.size() + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("导入失败：" + e.getMessage());
        }
    }
}


